export const commonMixin = {
  data() {
    return {
      loading: false, // 初始化加载状态
      listQuery: {
        pageNo: 1,
        pageSize: 10
      },
      totalCount: 0, // 分页参数
      tableData: [] // table项数据
    }
  },
  methods: {
    /**
     * tabel界面的增删改 全部封装成promise
     * @create 新增
     * @delete 删除
     * @update 修改
     * @getInfo 查询
     * @handlerSearch 点击查询按钮或者Enter模糊搜索
     */
    create() {
      return new Promise((resovle, reject) => {
        this.comApiUrlAdd.createOne(this.formData).then(res => {
          if (res.code === 0) {
            this.$message({
              type: 'success',
              message: '新增成功'
            })
            this.dialogFormVisible = false
            this.getList()
            this.$refs.formData.resetFields()
            resovle()
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
            reject()
          }
        })
      })
    },
    getInfo() {
      return new Promise((resovle, reject) => {
        this.loading = true
        const queryParams = Object.assign({}, this.listQuery)
        for (const key in queryParams) {
          !queryParams[key] ? delete queryParams[key] : ''
        }
        // console.log(queryParams)
        this.comApiUrlList.fetchList(queryParams).then(res => {
          // console.log(res)
          if (res.code === 0) {
            this.loading = false
            if (res.data) {
              this.tableData = res.data.list
              // console.log(this.tableData)
              this.totalCount = res.data.total
            } else {
              this.tableData = []
              this.totalCount = 0
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
            const routePath = this.$route.path
            // console.log(routePath)
            // 提成管理回显提成总金额
            if (routePath === '/business/percentage') {
              res.msg = res.msg.split(',')
              this.saleMoney = res.msg[0]
              this.saleProfile = res.msg[1]
            }
            resovle()
          } else {
            this.loading = false
            const routePath = this.$route.path
            if (routePath === '/business/percentage') {
              this.tableData = []
            }
            this.$message({
              type: 'error',
              message: res.msg
            })
            reject()
          }
        })
      })
    },
    delete(id) {
      return new Promise((resolve, reject) => {
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.comApiUrlDelete.deleteOne(id).then(res => {
            this.loading = false
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '删除成功'
              })
              resolve()
            } else {
              this.loading = false
              this.$message({
                type: 'error',
                message: res.msg
              })
              reject()
            }
            this.listQuery = {
              pageNo: 1,
              pageSize: 10
            }
            this.getList()
          })
        }).catch(e => e)
      })
    },
    update() {
      return new Promise((resolve, reject) => {
        this.$confirm('确认修改?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.comApiUrlUpdate.updateOne(this.formData).then(res => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '修改成功'
              })
              this.dialogFormVisible = false
              this.getList()
              this.$forceUpdate()
              this.$refs.formData.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
            resolve()
          })
        })
      })
    },
    // 初始化数据
    getList() {
      this.getInfo()
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReload() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 10
      }
      this.getList()
    },
    // 删除
    handleDelete(id) {
      this.delete(id)
    },
    // 关闭对话框
    handleClose() {
      this.dialogFormVisible = false
      this.$refs.formData.resetFields()
    },
    // 返回
    handleBack() {
      this.$router.go(-1)
    },
    // 对话框叉关闭事件
    closeDialog() {
      this.$refs.formData.resetFields()
    }
  }
}
